// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../script/game/control/game";
import { ccs } from "../../../script/util/services";
import { WinBase } from "../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;
let arr = [
    "power",
    "pressure",
    "lifeTime",
    "health",
    "charm",
]
@ccclass
export default class NewClass extends WinBase {

    @property(cc.Label)
    money: cc.Label = null;
    start () {
        ccs.gf.extendNodeName(this.nodes, this.node.parent);
        this.setInfo();
        this.setTime();
        this.registerEvent("updateTime", () => {
            this.setTime();
        })
        this.registerEvent("updateAttribute", () => {
            arr.forEach(val => {
                this.setProgress(this.nodes[val], val);
            })
            this.setProgress(this.nodes["power"], "power");
            this.money.string = "财富：" + ccs.gf.getOmitNum(game.playerMgr.heroInfo.money);
            this.nodes.age.getComponent(cc.Label).string = game.playerMgr.heroInfo.age + "岁";
        })
        //更新钱
        this.registerEvent("updateMoney", () => {
            this.nodes.wealth.getComponent(cc.Label).string = "财富：" +  ccs.gf.getOmitNum(game.playerMgr.heroInfo.money);
        })
        //更新寿命
        this.registerEvent('updateLife', () => {
            this.setProgress(this.nodes.lifeTime, "lifeTime");
        })
        this.touchEndEventScale(this.nodes.playerName, () => {
            ccs.winMgr.showWin("popAlert", {content: "需要观看广告改名，确定吗？", cb: () => {
                ccs.AD.showAd(() => {
                    ccs.winMgr.showWin('popEditor', {cb: (res) => {
                        game.playerMgr.heroInfo.name = res;
                        this.nodes.playerName.getComponent(cc.Label).string = res;
                    }}, )
                })
            }})
        })
    }

    setTime() {
        this.nodes.time.getComponent(cc.Label).string = game.timerMgr.getTime();
    }
    setInfo() {
        this.money.string = "财富" +  ccs.gf.getOmitNum(game.playerMgr.heroInfo.money);
        this.nodes.playerName.getComponent(cc.Label).string = game.playerMgr.heroInfo.name;
        this.nodes.age.getComponent(cc.Label).string = game.playerMgr.heroInfo.age.toString();
        arr.forEach(val => {
            this.setProgress(this.nodes[val], val);
        })
    }
    //更新属性
    setProgress(node: cc.Node, key: string) {
        let proNode = node.children[0];
        let all = 100;
        if(key === 'lifeTime') {
            all = 1200;
        } else if(key == "power") {
            all = 300;
        }
        proNode.getComponent(cc.ProgressBar).progress = game.playerMgr.heroInfo[key]/all;
        proNode.getChildByName("charmText").getComponent(cc.Label).string = parseInt(game.playerMgr.heroInfo[key]).toString();
    }

}
